/*******************************************************************/
/* @ All Rights Reserved Copyright (C) 2014 broada,Co.,Ltd.        */
/*                                                                 */
/*  NAME      = t__resp_parse.c                                    */
/*  NOTE      =                                                    */
/*  DATE      = 2016/03/31 by jikw                                 */
/*******************************************************************/

#include <CUnit/Basic.h>
#include <stdlib.h>

#include <dotnet_remoting/dotnet_remoting_core.h>
#include "frsutl.h"
#include "frcomm.h"
#include "frmem.h"
#include "conntrack.h"
#include "frct_comm.h"
#include "frreport_comm.h"
#include "../../../src/module/dotnet_remoting/config.h"

#include "frbyte_buffer.h"

typedef struct __st_L7session_dotnet_remoting_private
{
    unsigned long ul_seq;                             /*!< 序列号，1次req和response时，使用同一个序列号 */
    L4_transaction st_l4_trans;                       /*!< 数据 */
    Frreport_body_data *p_st_body_data_req;
    Frreport_body_data *p_st_body_data_resp;
    Frlist_header *p_lst_comm;
    Frlist_header *p_lst_req;
    Frbyte_Packet *p_req_packet;
    Frbyte_Packet *p_resp_packet;
}L7session_dotnet_remoting_private;


extern int __resp_parse(Frbyte_Packet *p_packet
                        , L7session_dotnet_remoting_private *p_st_private
                        , Frreport_body_data *p_st_body_data
                        , Conntrack *p_st_conn);
extern int dotnet_remoting_session_destroy(void *p_private_session);

static int __suite_init(void)
{

	return 0;
}

static int __suite_cleanup(void)
{

	return 0;
}

static void case1(void)
{
    int i_ret = 0;
    Frbyte_Packet *p_packet = NULL;
    L7session_dotnet_remoting_private *p_st_private = NULL;
    Frreport_body_data *p_st_body_data = NULL;
    Conntrack *p_st_conn = NULL;

    i_ret = __resp_parse(p_packet, p_st_private, p_st_body_data, p_st_conn);

    CU_ASSERT_EQUAL(i_ret, APM_L7_ANALYZE_FAIL);

    return;
}

static void case2(void)
{
    int i_ret = 0;
    char *p_data = NULL;
    Frbyte_Packet *p_packet = NULL;
    L7session_dotnet_remoting_private *p_st_private = NULL;
    Frreport_body_data *p_st_body_data = NULL;
    Conntrack *p_st_conn = NULL;

    p_data = (char *)"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x15\x12\x00\x00\x00\x12\x07\x67\x65\x74\x4e\x61\x6d\x65\x12\x5f\x52\x65\x6d\x6f\x74\x69\x6e\x67\x4f\x62\x6a\x65\x63\x74\x73\x2e\x49\x50\x65\x72\x73\x6f\x6e\x2c\x20\x52\x65\x6d\x6f\x74\x69\x6e\x67\x4f\x62\x6a\x65\x63\x74\x73\x2c\x20\x56\x65\x72\x73\x69\x6f\x6e\x3d\x31\x2e\x30\x2e\x30\x2e\x30\x2c\x20\x43\x75\x6c\x74\x75\x72\x65\x3d\x6e\x65\x75\x74\x72\x61\x6c\x2c\x20\x50\x75\x62\x6c\x69\x63\x4b\x65\x79\x54\x6f\x6b\x65\x6e\x3d\x6e\x75\x6c\x6c\x01\x00\x00\x00\x12\x08\x78\x69\x61\x6f\x6d\x69\x6e\x67\x0b";
    p_packet = bytebuff_creat();
    if(p_packet == NULL)
        goto ERROR;
    if(bytebuff_add(p_packet, p_data, 143) != FR_SUCC)
        goto ERROR;

    p_st_private = (L7session_dotnet_remoting_private *)frmalloc(sizeof(L7session_dotnet_remoting_private));
    if(p_st_private == NULL)
        goto ERROR;
    memset(p_st_private, 0x00, sizeof(L7session_dotnet_remoting_private));

    p_st_body_data = (Frreport_body_data *)frmalloc(sizeof(Frreport_body_data));
    if(p_st_body_data == NULL)
        goto ERROR;
    memset(p_st_body_data, 0x00, sizeof(Frreport_body_data));
    p_st_body_data->em_type = FRREPORT_TYPE_L7DATA;

    p_st_conn = (Conntrack *)frmalloc(sizeof(Conntrack));
    if(p_st_conn == NULL)
        goto ERROR;
    memset(p_st_conn, 0x00, sizeof(Conntrack));

    i_ret = __resp_parse(p_packet, p_st_private, p_st_body_data, p_st_conn);

    CU_ASSERT_EQUAL(i_ret, APM_L7_ANALYZE_FAIL);

    dotnet_remoting_session_destroy(p_st_private);
    frreport_body_free(&p_st_body_data);
    frfree(p_st_conn);

    return;

ERROR:
    if(p_st_private)
        dotnet_remoting_session_destroy(p_st_private);
    if(p_st_body_data)
        frreport_body_free(&p_st_body_data);
    if(p_st_conn)
        frfree(p_st_conn);
    if(p_packet)
        bytebuff_free(&p_packet);

    return;
}

static void case3(void)
{
    int i_ret = 0;
    char *p_data = NULL;
    Frbyte_Packet *p_packet = NULL;
    L7session_dotnet_remoting_private *p_st_private = NULL;
    Frreport_body_data *p_st_body_data = NULL;
    Conntrack *p_st_conn = NULL;

    p_data = (char *)"\x2e\x4e\x45\x54\x01\x00\x02\x00\x00\x00\x17\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x16\x11\x04\x00\x00\x0b";
    p_packet = bytebuff_creat();
    if(p_packet == NULL)
        goto ERROR;
    if(bytebuff_add(p_packet, p_data, 39) != FR_SUCC)
        goto ERROR;

    p_st_private = (L7session_dotnet_remoting_private *)frmalloc(sizeof(L7session_dotnet_remoting_private));
    if(p_st_private == NULL)
        goto ERROR;
    memset(p_st_private, 0x00, sizeof(L7session_dotnet_remoting_private));

    p_st_body_data = (Frreport_body_data *)frmalloc(sizeof(Frreport_body_data));
    if(p_st_body_data == NULL)
        goto ERROR;
    memset(p_st_body_data, 0x00, sizeof(Frreport_body_data));
    p_st_body_data->em_type = FRREPORT_TYPE_L7DATA;

    p_st_conn = (Conntrack *)frmalloc(sizeof(Conntrack));
    if(p_st_conn == NULL)
        goto ERROR;
    memset(p_st_conn, 0x00, sizeof(Conntrack));

    i_ret = __resp_parse(p_packet, p_st_private, p_st_body_data, p_st_conn);

    CU_ASSERT_EQUAL(i_ret, APM_L7_ANALYZE_SET_RESP(APM_L7_ANALYZE_SUCC));

    dotnet_remoting_session_destroy(p_st_private);
    frreport_body_free(&p_st_body_data);
    frfree(p_st_conn);

    return;

ERROR:
    if(p_st_private)
        dotnet_remoting_session_destroy(p_st_private);
    if(p_st_body_data)
        frreport_body_free(&p_st_body_data);
    if(p_st_conn)
        frfree(p_st_conn);
    if(p_packet)
        bytebuff_free(&p_packet);

    return;
}

static void case4(void)
{
    int i_ret = 0;
    char *p_data = NULL;
    Frbyte_Packet *p_packet = NULL;
    L7session_dotnet_remoting_private *p_st_private = NULL;
    Frreport_body_data *p_st_body_data = NULL;
    Conntrack *p_st_conn = NULL;

    p_data = (char *)"\x2e\x4e\x45\x54\x01\x00\x02\x00\x00\x00\x21\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x16\x11\x08\x00\x00\x12\x08\x78\x69\x61\x6f\x6d\x69\x6e\x67\x0b";
    p_packet = bytebuff_creat();
    if(p_packet == NULL)
        goto ERROR;
    if(bytebuff_add(p_packet, p_data, 49) != FR_SUCC)
        goto ERROR;

    p_st_private = (L7session_dotnet_remoting_private *)frmalloc(sizeof(L7session_dotnet_remoting_private));
    if(p_st_private == NULL)
        goto ERROR;
    memset(p_st_private, 0x00, sizeof(L7session_dotnet_remoting_private));

    p_st_body_data = (Frreport_body_data *)frmalloc(sizeof(Frreport_body_data));
    if(p_st_body_data == NULL)
        goto ERROR;
    memset(p_st_body_data, 0x00, sizeof(Frreport_body_data));
    p_st_body_data->em_type = FRREPORT_TYPE_L7DATA;

    p_st_conn = (Conntrack *)frmalloc(sizeof(Conntrack));
    if(p_st_conn == NULL)
        goto ERROR;
    memset(p_st_conn, 0x00, sizeof(Conntrack));

    i_ret = __resp_parse(p_packet, p_st_private, p_st_body_data, p_st_conn);

    CU_ASSERT_EQUAL(i_ret, APM_L7_ANALYZE_SET_RESP(APM_L7_ANALYZE_SUCC));

    dotnet_remoting_session_destroy(p_st_private);
    frreport_body_free(&p_st_body_data);
    frfree(p_st_conn);

    return;

ERROR:
    if(p_st_private)
        dotnet_remoting_session_destroy(p_st_private);
    if(p_st_body_data)
        frreport_body_free(&p_st_body_data);
    if(p_st_conn)
        frfree(p_st_conn);
    if(p_packet)
        bytebuff_free(&p_packet);

    return;
}

static void case5(void)
{
    int i_ret = 0;
    char *p_data = NULL;
    Frbyte_Packet *p_packet = NULL;
    L7session_dotnet_remoting_private *p_st_private = NULL;
    Frreport_body_data *p_st_body_data = NULL;
    Conntrack *p_st_conn = NULL;

    p_data = (char *)"\x2e\x4e\x45\x54\x00\x00\x02\x00\x00\x00\x21\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x16\x11\x08\x00\x00\x12\x08\x78\x69\x61\x6f\x6d\x69\x6e\x67\x0b";
    p_packet = bytebuff_creat();
    if(p_packet == NULL)
        goto ERROR;
    if(bytebuff_add(p_packet, p_data, 49) != FR_SUCC)
        goto ERROR;

    p_st_private = (L7session_dotnet_remoting_private *)frmalloc(sizeof(L7session_dotnet_remoting_private));
    if(p_st_private == NULL)
        goto ERROR;
    memset(p_st_private, 0x00, sizeof(L7session_dotnet_remoting_private));

    p_st_body_data = (Frreport_body_data *)frmalloc(sizeof(Frreport_body_data));
    if(p_st_body_data == NULL)
        goto ERROR;
    memset(p_st_body_data, 0x00, sizeof(Frreport_body_data));
    p_st_body_data->em_type = FRREPORT_TYPE_L7DATA;

    p_st_conn = (Conntrack *)frmalloc(sizeof(Conntrack));
    if(p_st_conn == NULL)
        goto ERROR;
    memset(p_st_conn, 0x00, sizeof(Conntrack));

    i_ret = __resp_parse(p_packet, p_st_private, p_st_body_data, p_st_conn);

    CU_ASSERT_EQUAL(i_ret, APM_L7_ANALYZE_FAIL);

    dotnet_remoting_session_destroy(p_st_private);
    frreport_body_free(&p_st_body_data);
    frfree(p_st_conn);

    return;

ERROR:
    if(p_st_private)
        dotnet_remoting_session_destroy(p_st_private);
    if(p_st_body_data)
        frreport_body_free(&p_st_body_data);
    if(p_st_conn)
        frfree(p_st_conn);
    if(p_packet)
        bytebuff_free(&p_packet);

    return;
}

int add_suite__resp_parse(void)
{
    CU_pSuite pSuite = NULL;

    if ((pSuite = CU_add_suite(MODULE_NAME" __resp_parse()", __suite_init, __suite_cleanup)) == NULL)
        return FR_FAIL;

    if (CU_add_test(pSuite, "case1", case1) == NULL ||
        CU_add_test(pSuite, "case2", case2) == NULL ||
        CU_add_test(pSuite, "case3", case3) == NULL ||
        CU_add_test(pSuite, "case4", case4) == NULL ||
        CU_add_test(pSuite, "case5", case5) == NULL)
        return FR_FAIL;

    return FR_SUCC;
}
